#include "fxcc/core/graph/opengl3/font/Effect.h"
#include "fxcc/core/graph/opengl3/font/Factory.h"
#include "mustache.hpp"

fxcc::graph::opengl3::font::Effect::Effect(const std::string& fragCode)
	:fxcc::graph::opengl3::Effect3d(fragCode )
{
	InitRenderDesc();

}

void fxcc::graph::opengl3::font::Effect::InitRenderDesc()
{
	m_PipeLineType.m_BlendType = fxcc::graph::common::BlendType::_FONT_;
	m_PipeLineType.m_DepthStencilType = fxcc::graph::common::DepthStencilType::_DEPTH_NONE_STENCIL_NONE_;
	m_PipeLineType.m_RasterizerType = fxcc::graph::common::RasterizerType::_NONE_;
}

void fxcc::graph::opengl3::font::Effect::Begin()
{
	fxcc::graph::opengl3::Effect3d::Begin();



}

void fxcc::graph::opengl3::font::Effect::RenderTextLine(const fxcc::graph::common::TextLine& inst)
{
	m_Shader->Use();
	m_Shader->setInt("text", 0);
	m_Shader->setVec4("textColor", inst.m_TextColor);

	auto& rs = fxcc::graph::opengl3::font::Factory::Inst();

	if (rs.HasFontFamily(inst.m_FontFamily))
	{
		auto fontLib = rs.m_FontLibs[inst.m_FontFamily];
		fontLib->RenderTextMesh(inst);
	}
	else
	{
		ztclog::info("Failed render font %s", inst.m_FontFamily.c_str());
	}
}

void fxcc::graph::opengl3::font::Effect::End()
{
	fxcc::graph::common::EffectBase::End();
}


std::string fxcc::graph::opengl3::font::Effect::DefaultragCode()
{
	return R"(
uniform vec4 textColor;
uniform sampler2D text;

out vec4 FragColor;

float GetSampled(){
	vec2 TexCoord= vOut.TexCoords;
    return texture(text, TexCoord).r;
};

void main()
{   
	float sampled = GetSampled(); 
	if(sampled<0.95f)
	{
		discard;
	}
	
    FragColor.xyz = textColor.xyz * sampled;
	FragColor.w=1.0f;
}
)";
}
